<?php
/**
 * @file tags.php
 * @author CaoQiCheng
 * @date 2017年2月8日 下午17:05:11
 * @brief 
 */
namespace haibao\jiepai\business;

use \haibao\jiepai\data\Tags as TagsData;
use \haibao\jiepai\model\filter\tag\CmsTags as CmsTagsFilter;
use haibao\frame\data\query\Condition;
use haibao\jiepai\business\BusinessException;
use haibao\jiepai\business\Tags as TagsBusiness;
use haibao\jiepai\model\filter\tag\CmsTagsRelation as CmsTagsRelationFilter;
use haibao\jiepai\model\data\tag\CmsTags as CmsTagsModel;

class Tags extends \haibao\jiepai\business\Base{	
	
	/**
	 * Tag与Tag2的关系。>为主从关系，<为从主关系，=为等价关系，≈为相关关系,∽为多媒体相关关系
	 */
	const LEVEL_GREATER_THAN = '>';
	const LEVEL_LESSER_THAN = '<';
	const LEVEL_EQUAL = '=';
	const LEVEL_APPROXIMATELY_EQUAL = '≈';
	const LEVEL_SIMILAR = '~';
	
	/**
	 * 删除时置为-1，默认是0
	 * 是否是删除
	 */
	const FLAG_IS = 0;
	const FLAG_DEL = -1;
	
	
	
	/**
	 * 合作列表
	 * @param unknown $query
	 */
	public static function getTags($query){
		$data = new TagsData();
		return $data->getTagsList($query);
	}
	
	public static function getTagNameByTagId($tagIds){
	    $data = new TagsData();
	    return $data->getTagNameByTagId($tagIds);
	}
	
	
	/**
	 * 查询一条
	 */
	public static function getOneTag($query){
		$data = new TagsData();
		return $data->getOne($query);
	}
	
	/**
	 * 保存标签
	 */
	public static function saveTags($tagsModel,$relationWord){
		$funcCode = $tagsModel->Id ? \haibao\cms\business\base::FUNC_CODE_APP_TAGS_EDIT : \haibao\cms\business\Base::FUNC_CODE_APP_TAGS_ADD;
		\haibao\cms\business\Base::checkOperation($funcCode);
		if(!$tagsModel->TagName){
			throw new BusinessException('标签名不能为空');
		}
	
		if(!$tagsModel->Id){
			if(in_array(\haibao\cms\business\base::getCurrentUser()->Id,array_keys(\haibao\jiepai\business\Base::$userMap))){
				$tagsModel->Creator = \haibao\jiepai\business\Base::$userMap[\haibao\cms\business\base::getCurrentUser()->Id]['id'];
			}else {
				throw new BusinessException('修改的编辑名没有加入cms_admin表');
			}
		}
		$tagsModel->CreateTime  = new \DateTime();
		$tagsModel->Flag = 0;
	
		$tagsData = new TagsData;
		return $tagsData->saveTags($tagsModel,$relationWord);
		\haibao\cms\business\Base::saveOperateHistory($funcCode, $tagsModel->toArray(true,true));
	}
	
	/**
	 * 根据ID取得tag名
	 */
	public static function getTagOnlyNameByTagId($tagIdsArr){
		$cmsTagsFilter = new CmsTagsFilter();
		$cmsTagsFilter->select(array(
				$cmsTagsFilter::CONDITION_FIELD_NAME_ID,
				$cmsTagsFilter::CONDITION_FIELD_NAME_TAG_NAME
		));
		$cmsTagsFilter->where($cmsTagsFilter::CONDITION_FIELD_NAME_ID, Condition::CONDITION_IN, $tagIdsArr);
		$data = new TagsData();
		$tagsList = $data->getTagsList($cmsTagsFilter);
		$tagNames = array();
		foreach ($tagsList as $key=>$val){
			$tagNames[$val->Id] = $val->TagName;
		}
		return $tagNames;
	}
	
	/**
	 * 根据tagId删除数据
	 */
	public static function delTagById($tagId){
		\haibao\cms\business\Base::checkOperation(\haibao\cms\business\Base::FUNC_CODE_APP_TAGS_DEL);
		if(!$tagId){
			throw new BusinessException('标签Id不能为空');
		}
		$data  = new TagsData();
		$data->delTagById($tagId);
		\haibao\cms\business\Base::saveOperateHistory(\haibao\cms\business\Base::FUNC_CODE_APP_TAGS_DEL, $tagId);
	}
	
	/**
	 * 根据ID取得tag详细信息
	 */
	public static function getTagInfoByTagId($tagId){
		$cmsTagsFilter = new CmsTagsFilter();
		$cmsTagsFilter->select(array(
				$cmsTagsFilter::CONDITION_FIELD_NAME_ID,
				$cmsTagsFilter::CONDITION_FIELD_NAME_TAG_NAME,
				$cmsTagsFilter::CONDITION_FIELD_NAME_CATEGORY_ID,
				$cmsTagsFilter::CONDITION_FIELD_NAME_CREATE_TIME,
				$cmsTagsFilter::CONDITION_FIELD_NAME_IS_CLASSIFY,
				$cmsTagsFilter::CONDITION_FIELD_NAME_CREATOR,
				$cmsTagsFilter::CONDITION_FIELD_NAME_FLAG
		));
		$cmsTagsFilter->where($cmsTagsFilter::CONDITION_FIELD_NAME_ID, Condition::CONDITION_EQUAL, (int)$tagId);
	
		$cmsTagsRelationFilter = new CmsTagsRelationFilter();
		$cmsTagsRelationFilter->select(array(
				$cmsTagsRelationFilter::CONDITION_FIELD_NAME_ID,
				$cmsTagsRelationFilter::CONDITION_FIELD_NAME_RELATION_LEVEL,
				$cmsTagsRelationFilter::CONDITION_FIELD_NAME_TAG_ID,
				$cmsTagsRelationFilter::CONDITION_FIELD_NAME_TAGID2,
				$cmsTagsRelationFilter::CONDITION_FIELD_NAME_WEIGHT
		));
	
		$cmsTagsFilter->leftJoin($cmsTagsRelationFilter,null,$cmsTagsFilter::CONDITION_FIELD_NAME_ID,$cmsTagsRelationFilter::CONDITION_FIELD_NAME_TAG_ID,$cmsTagsFilter::CONDITION_FIELD_NAME_TAGS_RELATION);
		$data = new TagsData();
		$tagsList = $data->getOneTag($cmsTagsFilter);
	
		return $tagsList;
	}

	public static function getHotRecommendTags(){

	    return \haibao\jiepai\cache\Crontab::get(\haibao\jiepai\cache\Crontab::LATELY_POPULAR_KEY);
    }
    /**
     * 
     * @param 标签类型 $category
     */
    public static function getTagsByCategory($category){
    	$data = new TagsData();
    	$tagsList = $data->getTagsByCategory($category);
    	return $tagsList;
    	
    }
}